package ink.zhaohui.bytebuddy.interceptor;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MonitorMethodTime {

  final Logger logger = LoggerFactory.getLogger(MonitorMethodTime.class);

  @RuntimeType
  public Object intercept(
      @Origin Method method, @AllArguments Object[] allArguments, @SuperCall Callable<?> callable)
      throws Exception {
    long t1 = System.currentTimeMillis();
    Object r = callable.call();
    logger.info("{}:{}", method.getName(), System.currentTimeMillis() - t1);
    return r;
  }
}
